<div class="accounts-page">
  {#if loading}
    <LoadingPage />
  {:elseif accounts && accounts.length}
    <ul class="accounts-results">
      {#each accounts as account}
        <AccountSearchResult
          {account}
          actions={accountActions}
          on:click="onClickAction(event)"
        />
      {/each}
    </ul>
  {/if}
</div>
<style>
  .accounts-page {
    padding: 20px 20px;
    position: relative;
  }
  .accounts-results {
    list-style: none;
    box-sizing: border-box;
    border: 1px solid var(--main-border);
    border-radius: 2px;
  }
  @media (max-width: 767px) {
    .accounts-page {
      padding: 20px 10px;
    }
  }
</style>
<script>
  import { store } from '../_store/store.js'
  import LoadingPage from './LoadingPage.html'
  import AccountSearchResult from './search/AccountSearchResult.html'
  import { toast } from './toast/toast.js'
  import { on } from '../_utils/eventBus.js'
  import { formatIntl } from '../_utils/formatIntl.js'

  // TODO: paginate
  export default {
    async oncreate () {
      try {
        await this.refreshAccounts()
      } catch (e) {
        /* no await */ toast.say(formatIntl('intl.error', { error: (e.message || '') }))
      } finally {
        this.set({ loading: false })
      }
      on('refreshAccountsList', this, () => this.refreshAccounts())
    },
    data: () => ({
      loading: true,
      accounts: [],
      accountActions: undefined
    }),
    store: () => store,
    components: {
      LoadingPage,
      AccountSearchResult
    },
    methods: {
      onClickAction (event) {
        const { action, accountId } = event
        action.onclick(accountId)
      },
      async refreshAccounts () {
        const { accountsFetcher } = this.get()
        const accounts = await accountsFetcher()
        this.set({ accounts })
      }
    }
  }
</script>
